package com.suzhe.ymall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suzhe.ymall.common.annotation.LoginAuth;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.product.pojo.Brand;
import com.suzhe.ymall.product.param.BrandPageParam;
import com.suzhe.ymall.product.pojo.Category;
import com.suzhe.ymall.product.pojo.CategoryBrand;
import com.suzhe.ymall.product.service.IBrandService;
import com.suzhe.ymall.product.service.ICategoryBrandService;
import com.suzhe.ymall.product.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-07-04
 */
@RestController
@RequestMapping("/product/brand")
@Slf4j
public class BrandController {


    @Autowired
    IBrandService iBrandService;

    @Autowired
    ICategoryBrandService iCategoryBrandService;

    @Autowired
    ICategoryService iCategoryService;


    @GetMapping("/page")
    @LoginAuth("productBrandView")
    public ApiResEntity page(BrandPageParam queryParam){
        Integer pageNo = queryParam.getPageNo();
        Integer pageSize = queryParam.getPageSize();
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(queryParam.getName())){
            queryWrapper.like("name",queryParam.getName());
        }
        queryWrapper.eq("enable",queryParam.isEnable());

        IPage<Brand> page = iBrandService.page(new Page<>(pageNo, pageSize),queryWrapper);
        return ApiResEntity.ok(page);
    }

    @GetMapping("/brandCategorys")
    public ApiResEntity brandCategorys(Brand brand){
        List<CategoryBrand> categoryBrands = iCategoryBrandService.list(//
                new QueryWrapper<CategoryBrand>().eq("brand_id", brand.getId()));
        List<Integer> categoryIds = categoryBrands.stream().map(item -> {
            return item.getCategoryId();
        }).collect(Collectors.toList());

        if (!categoryIds.isEmpty()){
            Collection<Category> categories = iCategoryService.listByIds(categoryIds);
            return ApiResEntity.ok(categories);
        }
        return ApiResEntity.ok(Collections.EMPTY_LIST);

    }

    @PostMapping("/save")
    @LoginAuth("productBrandSave")
    public ApiResEntity save(@RequestBody Brand brand){
        log.debug("save brand:{}",brand);
        iBrandService.saveBrand(brand);
        return ApiResEntity.ok();
    }

    @PutMapping("/update")
    @LoginAuth("productBrandUpdate")
    public ApiResEntity edit(@RequestBody Brand brand){
        log.debug("edit brand:{}",brand);
        iBrandService.saveBrand(brand);
        return ApiResEntity.ok();
    }

    @DeleteMapping("/delete")
    @LoginAuth("productBrandDelete")
    public ApiResEntity delete(@RequestBody Brand brand){
        log.debug("delete brand:{}",brand);
        iBrandService.removeById(brand.getId());
        return ApiResEntity.ok();
    }

    @GetMapping("/brandByCategoryId")
    public ApiResEntity brandByCategoryId(Long categoryId){
        List<CategoryBrand> categoryBrands = iCategoryBrandService.list(new //
                QueryWrapper<CategoryBrand>().eq("category_id", categoryId));
        List<Integer> brandIds = new ArrayList<>();
        categoryBrands.forEach(item -> {
            brandIds.add(item.getBrandId());
        });

        List<Brand> brands = (List)iBrandService.listByIds(brandIds);
        return ApiResEntity.ok(brands);
    }

}
